var app = getApp();

Page({
  data: {
    orderInfo: "",
    proId: "", //产品id
    categoryName: "",
    listStaus: "",
    peopleList: [],
    peopleVal: "",
    peoplePickerShow: false,
    imgUrl: app.globalData.imgUrl,
    apiUrl: "",
    intime: "",
    outtime: "",
    days: "",
    email: "",
    mobile: "",
    address: "",
    shadowFlag: false,
    systemInfo: {},
    padBtm: "25",
    topid:0
  },
  onLoad: function(e) {
    var that = this;
    if (e.topid) {
      var topid = e.topid;
      that.setData({
        topid: topid
      })
      console.log("topidd", topid)
    }
 
    wx.getSystemInfo({
      success: function(res) {
        if (res.model == "iPhone X") {
          that.setData({
            padBtm: "50",
          })
        }
      }
    });
    if (e.category) {
      that.setData({
        categoryName: e.category
      })
    }
    if (e.name == 'hotel') {
      that.setData({
        apiUrl: app.globalData.apiHost + "/api/index/house",
        peopleVal: e.peopleVal,
      })
    } else if (e.name == 'serve') {
      that.setData({
        apiUrl: app.globalData.apiHost + "/api/index/goodsDetails",
        yearVal: e.yearVal
      })
    }
    that.setData({
      navH: app.globalData.navHeight,
      proId: e.id,
      listStaus: e.name,
      intime: e.intime,
      outtime: e.outtime,
      // topid: topid
    })
    that.getProDet();
    that.getUserInfo();
  },
  getProDet: function() {
    var that = this;
    wx.request({
      url: that.data.apiUrl,
      data: {
        id: that.data.proId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var orderData = res.data.data;
          var maxNum = res.data.data.maximum;
          var numArr01 = [];
          var numArr02 = [];
          for (var i = 1; i <= maxNum; i++) {
            numArr01[i - 1] = i;
          }
          numArr02.push(numArr01);
          that.setData({
            orderInfo: orderData,
            peopleList: numArr02
          })
        }
      },

    });
  },
  getUserInfo: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/user/info",
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var userData = res.data.data;
          that.setData({
            email: userData.email,
            mobile: userData.mobile,
          })
        }
      },

    });
  },
  navBack: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  mobileInput: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  emailInput: function(e) {
    this.setData({
      email: e.detail.value
    })
  },
  addressInput: function(e) {
    this.setData({
      address: e.detail.value
    })
  },

  orderSubmit: function(e) {
    let formId = e.detail.formId;
    var that = this;
    var telReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var emailRge = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    
    if (e.detail.value.mobile.length == 0 || e.detail.value.email.length == 0) {
      wx.showToast({
        title: '手机号码和邮箱不得为空',
        icon: 'none',
        duration: 1500
      })
    } else if (e.detail.value.mobile.length != 11) {
      wx.showToast({
        title: '手机号码长度有误',
        icon: 'none',
        duration: 1500
      })
    } else if (!telReg.test(e.detail.value.mobile)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none',
        duration: 1500
      })
    } else if (!emailRge.test(e.detail.value.email)) {
      wx.showToast({
        title: '邮箱格式不正确',
        icon: 'none',
        duration: 1500
      })
    } else {
      if (that.data.listStaus == 'hotel') {
        wx.redirectTo({
          url: '../../template/notice/notice?id=' + that.data.proId +
            '&type=buy&number=1&order_type=1&intime=' + that.data.intime +
            '&outtime=' + that.data.outtime +
            '&people_num=' + that.data.peopleVal +
            '&mobile=' + e.detail.value.mobile +
            '&email=' + e.detail.value.email +
            '&days=' + e.detail.target.dataset.days +
            '&thumb=' + that.data.orderInfo.pic +
            '&form_id=' + formId 
        })
      } else {
        that.setData({
          shadowFlag: true
        });
        wx.request({
          url: app.globalData.apiHost + "/api/order/saveOrder",
          data: {
            goods_id: that.data.orderInfo.id,
            number: that.data.yearVal,
            order_type: that.data.orderInfo.category_id,
            intime: that.data.intime,
            outtime: that.data.outtime,
            mobile: e.detail.value.mobile,
            email: e.detail.value.email,
            address: e.detail.value.address,
            form_id: formId
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Cookie': 'token=' + wx.getStorageSync('token')
          },
          method: "POST",
          dataType: "json",
          success: function(res) {
            if (res.statusCode === 200) {
              that.setData({
                shadowFlag: false,
                orderId: res.data.data
              })
              wx.redirectTo({
                url: '/pages/template/reserve/reserve?id=2&orderId=' + that.data.orderId + '&orderType=' + that.data.orderInfo.category_id + '&thumb=' + that.data.orderInfo.thumb
              })
            }
          },
          fail: function(e) {},
          complete: function(obj) {}
        });
      }

    }

  }
})